




//
//  NewsRemindVC.m
//  XJMCustomer
//
//  Created by Mr.hu on 2017/10/10.
//  Copyright © 2017年 Mr.hu. All rights reserved.
//

#import "NewsRemindVC.h"
#import "NewsRemindTVCell.h"
#import "NewsRemindTVModel.h"
#import "RemindNewsAlert.h"

static NSString * onlypopular = @"popularonly";
@interface NewsRemindVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableview;
/** 注释 **/
@property (strong, nonatomic) UIView * vcstay;
/** 注释 **/
@property (strong, nonatomic) NSArray * arrdata;
/** 注释 **/
@property (assign, nonatomic) NSInteger RemindInte;
/** 注释 **/
@property (assign, nonatomic) NSInteger RemindIndexRow;
/** 注释 **/
@property (strong, nonatomic) RemindNewsAlert * remindnews;
/** 注释 **/
@property (strong, nonatomic) NSMutableArray * arrDictall;
@property (nonatomic,strong)  UIView * backview;

@end

@implementation NewsRemindVC

- (void)requestServerData{
    
    [XJHttpClass noticeMessagepage:@"1" andFinishblock:^(id response, NSString *errStr) {
        
        if (errStr) {
            
            self.backview.hidden = NO;
        }else{
            
            NSMutableArray * arrModel = [NSMutableArray array];
            NSDictionary * dictdata = response[@"data"];
            
            if ([dictdata isKindOfClass:[NSDictionary class]]) {
                
                self.backview.hidden = YES;
                self.arrDictall = dictdata[@"list"];
                for (NSDictionary * dic in self.arrDictall) {
                    NewsRemindTVModel * newsmode = [NewsRemindTVModel modelWithDict:dic];
                    [arrModel addObject:newsmode];
                }
            }else{
                self.backview.hidden = NO;
            }
            
            if (arrModel.count > 0) {
                self.arrdata = arrModel;
            }
            
            [self.tableview reloadData];
            
                if (_arrdata) {
                _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
                
            }else{
                
                UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"暂无消息" preferredStyle:UIAlertControllerStyleAlert];
                [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timexuanzhe:) userInfo:alert repeats:YES];
                [self presentViewController:alert animated:YES completion:nil];
                _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
                
            }
        }
    }];
}

-(void)timexuanzhe:(NSTimer*)theTimer
{
    UIAlertController * promptAlert =[theTimer userInfo];
    [promptAlert dismissViewControllerAnimated:NO completion:nil];
    promptAlert = NULL;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:NO];
    self.arrdata = [NSMutableArray array];
    self.arrDictall = [NSMutableArray array];
    
    [self addLeftBtn:[UIImage imageNamed:@"fanhui"] Action:@selector(backToPrevious)];
    self.title = @"消息";
    
     [self requestServerData];
    
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableview];
    [self.tableview registerClass:[NewsRemindTVCell class] forCellReuseIdentifier:onlypopular];
    
    self.backview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.backview.backgroundColor = RGBA(243, 243, 243, 1);
    [self.view addSubview:self.backview];
    
    UIImageView * topimageview = [[UIImageView alloc]init];
    [topimageview setImage:[UIImage imageNamed:@"no_messgae"]];
    [self.backview addSubview:topimageview];
    [topimageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(150);
        make.size.mas_equalTo(CGSizeMake(120, 120));
        make.centerX.mas_equalTo(0);
    }];
    UILabel * sizelabe = [[UILabel alloc]init];
    sizelabe.text = @"暂无消息";
    sizelabe.font = [UIFont systemFontOfSize:16];
    sizelabe.textColor = [UIColor grayColor];
    sizelabe.textAlignment = NSTextAlignmentCenter;
    [self.backview addSubview:sizelabe];
    [sizelabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topimageview.mas_bottom).offset(10);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
}

#pragma mark - TableViewDatasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrdata.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NewsRemindTVCell * friend=[tableView dequeueReusableCellWithIdentifier:onlypopular forIndexPath:indexPath];
        
        if(friend==nil){
            friend=[[NewsRemindTVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:onlypopular];
        }
        
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        friend.userInteractionEnabled = YES;
    
        friend.NewsRemindModel = self.arrdata[indexPath.row];
    
    
        return friend;
    
}

#pragma mark TableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当手指离开某行时，就让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.remindnews = [[RemindNewsAlert alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.remindnews.backgroundColor = RGBA(95, 94, 86, 0.8);
    self.remindnews.bindingexitbtn.tag = indexPath.row;
    self.remindnews.leftToplab.text = self.arrDictall[indexPath.row][@"title"];
    self.remindnews.reminddetaillab.text = self.arrDictall[indexPath.row][@"content"];
    
    [self.remindnews.bindingexitbtn addTarget:self action:@selector(bindingroutOnclicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.remindnews.bingdcancelbtn addTarget:self action:@selector(bindingCancelOnclicked) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:self.remindnews];
    
    [XJHttpClass ReadnoticeMessagemsg_id:self.arrDictall[indexPath.row][@"m_id"] andFinishblock:^(id response, NSString *errStr) {
        if (errStr) {
           
        }else{
            [self.tableview reloadData];
        }
    }];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jkr_tapGestureAction:)];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.remindnews addGestureRecognizer:tapGestureRecognizer];
    
}

- (void)jkr_tapGestureAction:(UITapGestureRecognizer *)tapGusture{
    
    [self.remindnews removeFromSuperview];
}

//逐条显示
- (void)bindingroutOnclicked:(UIButton *)sender{
    
    self.RemindInte++;
    NSInteger totalinte = self.arrDictall.count;
    if (((totalinte-self.RemindIndexRow)-self.RemindInte)>0) {
        self.remindnews.leftToplab.text = self.arrDictall[self.RemindIndexRow + _RemindInte][@"title"];
        self.remindnews.reminddetaillab.text = self.arrDictall[self.RemindIndexRow+_RemindInte][@"content"];
        
        [XJHttpClass ReadnoticeMessagemsg_id:self.arrDictall[sender.tag][@"m_id"] andFinishblock:^(id response, NSString *errStr) {
            if (errStr) {
//                [XJToolClass tipAnimationWith:errStr];
            }else{
                
            }
        }];
    }else{
        
        [XJHttpClass ReadnoticeMessagemsg_id:self.arrDictall[sender.tag][@"m_id"] andFinishblock:^(id response, NSString *errStr) {
            if (errStr) {
//                [XJToolClass tipAnimationWith:errStr];
            }else{
                
            }
        }];
    }
}

- (void)bindingCancelOnclicked{
    
    self.RemindInte = 0;
    [self.remindnews removeFromSuperview];
    [self requestServerData];
    [self.tableview reloadData];
}


@end
